package com.dao.impl;

import com.dao.ResourcesDao;
import com.pojo.Button;
import com.pojo.Resources;
import com.util.BaseJdbc;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ResourcesDaoImpl implements ResourcesDao {
    @Override
    public List<Resources> getResourcesAll(int uid) {
        List<Resources> list = new ArrayList<Resources>();
        try {
            String sql="SELECT * FROM`ly_resources` WHERE id IN(" +
                    " SELECT resid FROM `ly_role_res` " +
                    "  WHERE roleid IN(SELECT roleid FROM `ly_user_role` " +
                    "      WHERE userId=?)" +
                    ") AND TYPE IN(0,1) ";
            Object[] objects = new Object[1];
            objects[0]=uid;
            ResultSet res = BaseJdbc.BaseSelect(sql, objects);
            while (res.next()){
                Resources resources = new Resources();
                resources.setId(res.getInt("id"));
                resources.setResName(res.getString("name"));
                resources.setParendId(res.getInt("parentId"));
                resources.setType(res.getInt("type"));
                resources.setResUrl(res.getString("resUrl")+"&mid="+res.getInt("id"));
                list.add(resources);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Resources> getResourcesAll() {
        List<Resources> list = new ArrayList<Resources>();
        try {
            String sql="SELECT * FROM`ly_resources`";
            Object[] objects = new Object[1];
            ResultSet res = BaseJdbc.BaseSelect(sql, objects);
            while (res.next()){
                Resources resources = new Resources();
                resources.setId(res.getInt("id"));
                resources.setResName(res.getString("name"));
                resources.setParendId(res.getInt("parentId"));
                resources.setType(res.getInt("type"));
                resources.setResKey(res.getString("resKey"));
                resources.setResUrl(res.getString("resUrl")+"&mid="+res.getInt("id"));
                list.add(resources);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Resources> getResourcesBtnAll(int uid, int mid) {
        List<Resources> list = new ArrayList<Resources>();
        try {
            String sql="SELECT * FROM`ly_resources` WHERE id IN(" +
                    "SELECT resid FROM `ly_role_res` " +
                    "  WHERE roleid IN(SELECT roleid FROM `ly_user_role` " +
                    "      WHERE userId=?)" +
                    ") AND TYPE IN(2) AND parentId=?";
            Object[] objects = new Object[2];
            objects[0]=uid;
            objects[1]=mid;
            ResultSet res = BaseJdbc.BaseSelect(sql, objects);
            while (res.next()){
                Resources resources = new Resources();
                resources.setId(res.getInt("id"));
                resources.setResName(res.getString("name"));
                resources.setParendId(res.getInt("parentId"));
                resources.setType(res.getInt("type"));
                resources.setResUrl(res.getString("resUrl"));
                list.add(resources);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Resources> getUserMenu(int uid, int type) {
        List<Resources> list = new ArrayList<Resources>();
        try {
            String sql="SELECT * FROM`ly_resources` WHERE id IN(" +
                    "SELECT resid FROM `ly_role_res` " +
                    "  WHERE roleid IN(SELECT roleid FROM `ly_user_role` " +
                    "      WHERE userId=?)" +
                    ") AND TYPE IN(?);";
            Object[] objects = new Object[2];
            objects[0]=uid;
            objects[1]=type;
            ResultSet res = BaseJdbc.BaseSelect(sql, objects);
            while (res.next()){
                Resources resources = new Resources();
                resources.setId(res.getInt("id"));
                resources.setResName(res.getString("name"));
                resources.setParendId(res.getInt("parentId"));
                resources.setType(res.getInt("type"));
                resources.setResUrl(res.getString("resUrl"));
                resources.setIcon(res.getString("icon"));
                list.add(resources);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Resources> getMenuByType(int type) {
        List<Resources> list = new ArrayList<Resources>();
        try {
            String sql="SELECT * FROM`ly_resources` where type=?";
            Object[] objects = new Object[1];
            objects[0]=type;
            ResultSet res = BaseJdbc.BaseSelect(sql, objects);
            while (res.next()){
                Resources resources = new Resources();
                resources.setId(res.getInt("id"));
                resources.setResName(res.getString("name"));
                resources.setParendId(res.getInt("parentId"));
                resources.setType(res.getInt("type"));
                resources.setResUrl(res.getString("resUrl"));
                resources.setIcon(res.getString("icon"));
                list.add(resources);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Resources getMenuById(int mid) {
        Resources resources = new Resources();
        try {
            String sql="SELECT * FROM `ly_resources` where id=?";
            Object[] objects = new Object[1];
            objects[0]=mid;
            ResultSet res = BaseJdbc.BaseSelect(sql, objects);
            while (res.next()){
                resources.setId(res.getInt("id"));
                resources.setResName(res.getString("name"));
                resources.setParendId(res.getInt("parentId"));
                resources.setType(res.getInt("type"));
                resources.setResKey(res.getString("resKey"));
                resources.setResUrl(res.getString("resUrl"));
                resources.setIcon(res.getString("icon"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resources;
    }

    @Override
    public List<Button> getButtonAll() {
        List<Button> list = new ArrayList<Button>();
        try {
            String sql="SELECT * FROM `ly_buttom` ";
            Object[] objects = new Object[1];
            ResultSet res = BaseJdbc.BaseSelect(sql, objects);
            while (res.next()){
                Button button = new Button();
                button.setId(res.getInt("id"));
                button.setButton(res.getString("buttom"));
                list.add(button);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int addResources(Resources resources) {
        int i =0;
        try{
            String sql="INSERT INTO ly_resources(NAME,parentId,resKey,TYPE,resUrl,icon)VALUE(?,?,?,?,?,?)";
            Object[] objects = new Object[6];
            objects[0]=resources.getResName();
            objects[1]=resources.getParendId();
            objects[2]=resources.getResKey();
            objects[3]=resources.getType();
            objects[4]=resources.getResUrl();
            objects[5]=resources.getIcon();
            i = BaseJdbc.BaseUpdate(sql, objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }
}
